package com.wanrue.tcc.admin.util;

import java.util.HashMap;
import java.util.Map;

/**
 * controller 统一返回数据类
 * @author wenjie
 */
public class Result extends HashMap<String, Object> {

    private static final long serialVersionUID = 1887268515423257846L;

    private Map<Object, Object> dataItem = new HashMap<Object, Object>();

    public Result() {
        this.put("code", 200);
        this.put("mes", "sucess");
        this.put("data", null);
    }

    public Result(int code, String mes, Object data) {
        this.put("code", code);
        this.put("mes", mes);
        this.put("data", data);
    }

    public void setCode(int code) {
        this.put("code", code);
    }

    public void setMes(String mes) {
        this.put("mes", mes);
    }

    public void setData(Object data) {
        this.put("data", data);
    }

    public void putData(String key, Object val) {
        dataItem.put(key, val);
        Object data = this.get("data");
        if (!(data instanceof Map)) this.put("data", dataItem);
    }

    public void putAll(Map map) {
        dataItem.putAll(map);
        Object data = this.get("data");
        if (!(data instanceof Map)) this.put("data", dataItem);
    }

    public static Result SUCCESS() {
        return new Result(200, "sucess", null);
    }

    public static Result SUCCESS(Object data) {
        return new Result(200, "sucess", data);
    }

    public static Result ERROR() {
        return new Result(500, "erro", null);
    }

}